package com.yra.service.impl;

import com.yra.controller.vo.NewBeeMallIndexCategoryVO;
import com.yra.controller.vo.SecondLevelCategoryVO;
import com.yra.controller.vo.ThirdLevelCategoryVO;
import com.yra.domain.Category;
import com.yra.mapper.CategoryMapper;
import com.yra.service.CategoryService;
import com.yra.util.PageQueryUtil;
import com.yra.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yrq
 * @Date: Create in 9:08 2019/12/2
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Category> categoryByLevel(int categoryLevel) {
        return categoryMapper.categoryByLevel(categoryLevel);
    }

    @Override
    public void categoryInsert(Category category) {
        categoryMapper.categoryInsert(category);
    }

    @Override
    public void categoryDelete(int ids) {
        categoryMapper.categoryDelete(ids);
    }

    @Override
    public PageResult categoryListPage(PageQueryUtil pageQueryUtil) {
        String key="categories";
        ValueOperations<String, List<Category>> operations = redisTemplate.opsForValue();

        boolean hasKey = redisTemplate.hasKey(key);
        List<Category> goodsCategories=null;
        if (hasKey) {
            goodsCategories = operations.get(key);
        } else {
            goodsCategories = categoryMapper.categoryListPage(pageQueryUtil);
            // 写入缓存
            operations.set(key, goodsCategories, 1, TimeUnit.HOURS);
        }

        int total = categoryMapper.getTotalCategories(pageQueryUtil);
        PageResult pageResult = new PageResult(goodsCategories, total, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public void categoryUpdate(Category category) {
        categoryMapper.categoryUpdate(category);
    }

    @Override
    public List<Category> categoryListByLevelAndParentId(int categoryLevel, int parentId) {
        return categoryMapper.categoryListByLevelAndParentId(categoryLevel,parentId);
    }

    @Override
    public Category categoryListByCategoryId(int categoryId) {
        return categoryMapper.categoryListByCategoryId(categoryId);
    }

    @Override
    public void changeProductStatusById(int status, int[] ids) {
        categoryMapper.changeProductStatusById(status,ids);
    }

    @Override
    public List<NewBeeMallIndexCategoryVO> indexCategoryByLevel() {

        List<Category> categories = categoryMapper.categoryListByLevelAndParentId(1,0);
        List<NewBeeMallIndexCategoryVO> newBeeMallIndexCategoryVOS=new ArrayList<>();
        for (Category category:categories){
            NewBeeMallIndexCategoryVO newBeeMallIndexCategoryVO = new NewBeeMallIndexCategoryVO();
            newBeeMallIndexCategoryVO.setCategoryId(category.getCategoryId());
            newBeeMallIndexCategoryVO.setCategoryLevel(category.getCategoryLevel());
            newBeeMallIndexCategoryVO.setCategoryName(category.getCategoryName());
            List<Category> categories1 = categoryMapper.categoryListByLevelAndParentId(2, category.getCategoryId());
            List<SecondLevelCategoryVO> secondLevelCategoryVOS=new ArrayList<>();
            for (Category category1:categories1){
                SecondLevelCategoryVO secondLevelCategoryVO = new SecondLevelCategoryVO();
                secondLevelCategoryVO.setCategoryId(category1.getCategoryId());
                secondLevelCategoryVO.setCategoryLevel(category1.getCategoryLevel());
                secondLevelCategoryVO.setParentId(category1.getParentId());
                secondLevelCategoryVO.setCategoryName(category1.getCategoryName());
                secondLevelCategoryVOS.add(secondLevelCategoryVO);
                List<Category> categories2 = categoryMapper.categoryListByLevelAndParentId(3, category1.getCategoryId());
                List<ThirdLevelCategoryVO> thirdLevelCategoryVOS=new ArrayList<>();
                for (Category category2:categories2){
                    ThirdLevelCategoryVO thirdLevelCategoryVO = new ThirdLevelCategoryVO();
                    thirdLevelCategoryVO.setCategoryId(category2.getCategoryId());
                    thirdLevelCategoryVO.setCategoryLevel(category2.getCategoryLevel());
                    thirdLevelCategoryVO.setCategoryName(category2.getCategoryName());
                    thirdLevelCategoryVOS.add(thirdLevelCategoryVO);
                }
                secondLevelCategoryVO.setThirdLevelCategoryVOS(thirdLevelCategoryVOS);
            }
            newBeeMallIndexCategoryVO.setSecondLevelCategoryVOS(secondLevelCategoryVOS);
            newBeeMallIndexCategoryVOS.add(newBeeMallIndexCategoryVO);
        }
        return newBeeMallIndexCategoryVOS;
    }


}
